/*
 * Blower.cpp
 *
 *  Created on: Jul 13, 2009
 *      Author: scott
 */
#include <allegro.h>
#include "Blower.h"
#include "Dust.h"


Blower::Blower(SAMPLE * sample, bool suck, SceneGraphNode * parent, bool ownsChildren) :
	Weapon(parent, "media/blowercolour.3ds", ownsChildren){
	setTranslation(irr::core::vector3df(-0.2f, -0.3f, 1.5f));
	setScale(irr::core::vector3df(0.001f, 0.001f, 0.001f));
	setRotation(irr::core::vector3df(-70.0f, 15.0f, 0.0f));
	this->suck = suck;
	this->sample = sample;
}

Blower::~Blower() {
	// TODO Auto-generated destructor stub
}

void Blower::switchTo()
{

}

void Blower::use()
{
	// trigger the blower
	for (irr::core::list<Dust*>::Iterator iter = Dust::dust.begin();
			iter != Dust::dust.end(); iter++)
	{
		float distSquared = (*iter)->getAbsTranslation().getDistanceFromSQ(getAbsTranslation());

		if (suck && distSquared < 1)
		{
			//(*iter)->removeFromParent();
			//delete (*iter);
			(*iter)->setVisible(false);
			iter = Dust::dust.erase(iter);
			if (iter == Dust::dust.end())
			{
				break;
			}
			continue;
		}

		if (distSquared < 1000)
		{
			irr::core::vector3df dir = getAbsTranslation() - (*iter)->getAbsTranslation();
			dir.normalize();
			dir.Y = 0;

			/*if (iter == Dust::dust.begin())
			{
				cout << "Angle to object " << dir.getHorizontalAngle().Y << endl;
				cout << "Angle of weapon " << getAbsRotation().Y << endl;
			}*/

			/*while (angle > 360)
			{
				angle -= 360;
			}

			while (angle < 0)
			{
				angle += 360;
			}*/


			if (!sample)
			{
				//allegro_message("Error reading wave file");
			}

			//play the sample with looping
			stop_sample(sample);
			play_sample(sample, 128,128,1000, FALSE);

			if (suck)
			{
				dir *= 1000000.0f/pow(distSquared,2);
			}
			else
			{
				dir *= -1000000.0f/pow(distSquared,2);
			}
			dBodyAddForce((*iter)->getBody(), dir.X, dir.Y, dir.Z);
		}
	}
}

void Blower::switchFrom()
{

}
